package com.cjq.controller;

import com.cjq.bean.*;
import com.cjq.mvc.ResponseBody;
import com.cjq.service.CourierService;
import com.cjq.service.UserService;
import com.cjq.util.DateFormatUtil;
import com.cjq.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = UserService.console();

        Message msg = new Message();
        if (data.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }
    
    

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        // 获取查询数据的起始索引值
        int offset = Integer.parseInt(req.getParameter("offset"));
        //每一页查询的数量
        int pageNumber = Integer.parseInt(req.getParameter("limit"));

        List<User> list = UserService.findAll(true, offset, pageNumber); 
        List<BootStrapTableUser> list2 = new ArrayList<>();
        for (User u : list) {
            String createTime = u.getCreateTime()==null?"未有注册记录":DateFormatUtil.format(u.getCreateTime());
            String loginTime = u.getLoginTime()==null?"未有登录记录":DateFormatUtil.format(u.getLoginTime());
            BootStrapTableUser u1 = new BootStrapTableUser(u.getId(), u.getUserName(), u.getUserPhone(), u.getPassword(), createTime, loginTime);
            list2.add(u1);
        }

        //获取总条数
        List<Map<String, Integer>> console = UserService.console();    //这里获得的是一个map类型的集合，但实际只有一个map 这个map有两条数据 快递员总人数：n  今日注册人数：m
        Integer total = console.get(0).get("data1_size");

        //将集合为 bootstrap-table识别的格式
        ResultDate<BootStrapTableUser> data = new ResultDate<>();
        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String password = req.getParameter("password");

        User user = new User(userName,userPhone,password);
        boolean flay = UserService.insert(user);
        Message msg = new Message();
        if (flay){
            //录入成功
            msg.setStatus(0);
            msg.setResult("新用户录入成功");
        }else{
            //录入失败
            msg.setStatus(-1);
            msg.setResult("新用户录入失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/findByUserPhone.do")
    public String findByUserPhone(HttpServletRequest req, HttpServletResponse resp){
        String userPhone = req.getParameter("userPhone");
        User u = UserService.findByUserPhone(userPhone);
        Message msg = new Message();
        System.out.println(u);
        if (u==null){
            msg.setStatus(-1);
            msg.setResult("查询的电话不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(u);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String password = req.getParameter("password");
        User user = new User(id, userName, userPhone, password);
        boolean flay = UserService.update(user);
        Message msg = new Message();
        if (flay){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }

        return JSONUtil.toJSON(msg);

    }


    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean flay = UserService.delete(id);
        Message msg = new Message();
        if (flay){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJSON(msg);
    }
}
